package com.cirnoworks.guice;

import com.google.inject.ConfigurationException;
import com.google.inject.Injector;

public abstract class InjectorCreator {
	protected abstract Injector createInjector();

	public void shutdownInjector(Injector injector) {
		try {
			injector.getInstance(LifeCycleService.class).shutdown();
		} catch (ConfigurationException e) {
			// Ignore if the LifeCycleModule is not injected
		}
	}

	public void launchWithShutdownHook() {
		final Injector injector = createInjector();
		Runtime.getRuntime().addShutdownHook(new Thread() {
			public void run() {
				shutdownInjector(injector);
			}
		});
	}
}
